Skip to main content

sidebarPath setting in Docusaurus

The sidebarPath setting in Docusaurus is a crucial configuration option that defines the location of the sidebar configuration file for your documentation. This setting allows you to customize how your documentation is organized in the sidebar of your Docusaurus site. Here’s a detailed explanation of how it works:

Overview of sidebarPath

  • Purpose: The sidebarPath specifies the path to a JavaScript or JSON file that contains the structure of your sidebar. This file defines how the documentation sections, categories, and individual documents are organized and displayed in the sidebar navigation.

How to Use sidebarPath

  1. File Location:

    • The sidebarPath value should point to a file, typically named sidebars.js, sidebars.json, or similar, located in your project directory.

    • Example:

      sidebarPath: './sidebars.js',
  2. Sidebar Structure:

    • The referenced file should export an object or an array that describes the sidebar's structure.
    • You can create sections, categories, and specify which documents belong to each category.
  3. Example of sidebars.js:

    module.exports = {
    someSidebar: {
    'Introduction': ['intro'], // Intro document
    'Tutorials': [
    {
    type: 'category',
    label: 'Getting Started',
    items: ['tutorial-basics/create-a-blog-post', 'tutorial-basics/create-a-document'],
    },
    {
    type: 'category',
    label: 'Advanced Tutorials',
    items: ['tutorial-advanced/advanced-usage'],
    },
    ],
    },
    };
    • In this example:
      • someSidebar: The name of the sidebar configuration.
      • Introduction: A section that links to an introductory document.
      • Tutorials: A category that contains two subcategories, each with its own items (documents).
  • Rendering: When you navigate to your documentation site, Docusaurus will read the sidebarPath setting, load the specified file, and render the sidebar based on the structure defined inside it.
  • Dynamic Updates: If you add or modify documents in your documentation folder, you can easily update the sidebar by changing the sidebars.js file without needing to change the document files themselves.
  • Nested Categories: You can create nested categories in the sidebar to organize your documents hierarchically, making it easier for users to navigate through complex documentation.

Benefits of Using sidebarPath

  • Customization: It provides flexibility in how your documentation is presented, allowing you to tailor the navigation to the needs of your users.
  • Clarity: By structuring your sidebar clearly, users can find the information they need more quickly, improving the overall user experience.
  • Separation of Concerns: Keeping sidebar configuration separate from document content allows for easier maintenance and cleaner organization.

Conclusion

The sidebarPath setting in Docusaurus is a powerful feature that enables you to create a well-organized and user-friendly navigation structure for your documentation. By defining your sidebar in a dedicated configuration file, you gain the ability to customize how your documentation is presented, making it easier for users to explore and find the information they need.

==================

To create nested categories within your Docusaurus sidebar, you can structure your sidebars.js file to define categories and subcategories. Here’s a step-by-step guide on how to do this:

Step 1: Define Your Sidebar Structure

  1. Create or Open sidebars.js: If you haven’t already, create a sidebars.js file in the root of your Docusaurus project, or open the existing one.

  2. Use the Category Type: Within your sidebar configuration, use the type: 'category' to define categories and their nested items.

Example of Nested Categories

Here’s an example structure that demonstrates how to create nested categories:

module.exports = {
someSidebar: {
'Getting Started': [
// Top-level category
'intro', // Single document
{
type: 'category',
label: 'Installation', // Category label
items: [
'installation/quickstart', // Document in the Installation category
'installation/advanced', // Another document
],
},
{
type: 'category',
label: 'Usage', // Another top-level category
items: [
{
type: 'category',
label: 'Basic Usage', // Nested category
items: [
'usage/basic-commands', // Document in the nested category
'usage/basic-tips',
],
},
{
type: 'category',
label: 'Advanced Usage', // Another nested category
items: [
'usage/advanced-features',
'usage/performance-tuning',
],
},
],
},
],
'API Reference': [
{
type: 'category',
label: 'Endpoints',
items: [
'api/user-endpoints',
'api/admin-endpoints',
],
},
],
},
};

Step 2: Explanation of the Structure

  • Top-Level Categories: Each main category (like Getting Started and API Reference) can directly contain documents or other categories.

  • Nested Categories:

    • You can create nested categories by defining an object with type: 'category' and a label.
    • Inside this object, use the items array to list either documents or additional nested categories.
  • Document Links: Each item in the items array can be a string representing the document path, or another category object.

Step 3: Using the Sidebar

Once you’ve defined your sidebar structure, Docusaurus will automatically render it when you build and serve your site. Users will see a clear hierarchy in the sidebar, allowing them to expand and collapse categories for easier navigation.

Conclusion

By structuring your sidebars.js file appropriately, you can create a well-organized sidebar with nested categories in Docusaurus. This enhances the user experience by making it easier to find related documentation within a logical hierarchy.

======================

To create nested categories within your Docusaurus sidebar, you can structure your sidebars.js file to define categories and subcategories. Here’s a step-by-step guide on how to do this:

Step 1: Define Your Sidebar Structure

  1. Create or Open sidebars.js: If you haven’t already, create a sidebars.js file in the root of your Docusaurus project, or open the existing one.

  2. Use the Category Type: Within your sidebar configuration, use the type: 'category' to define categories and their nested items.

Example of Nested Categories

Here’s an example structure that demonstrates how to create nested categories:

module.exports = {
someSidebar: {
'Getting Started': [
// Top-level category
'intro', // Single document
{
type: 'category',
label: 'Installation', // Category label
items: [
'installation/quickstart', // Document in the Installation category
'installation/advanced', // Another document
],
},
{
type: 'category',
label: 'Usage', // Another top-level category
items: [
{
type: 'category',
label: 'Basic Usage', // Nested category
items: [
'usage/basic-commands', // Document in the nested category
'usage/basic-tips',
],
},
{
type: 'category',
label: 'Advanced Usage', // Another nested category
items: [
'usage/advanced-features',
'usage/performance-tuning',
],
},
],
},
],
'API Reference': [
{
type: 'category',
label: 'Endpoints',
items: [
'api/user-endpoints',
'api/admin-endpoints',
],
},
],
},
};

Step 2: Explanation of the Structure

  • Top-Level Categories: Each main category (like Getting Started and API Reference) can directly contain documents or other categories.

  • Nested Categories:

    • You can create nested categories by defining an object with type: 'category' and a label.
    • Inside this object, use the items array to list either documents or additional nested categories.
  • Document Links: Each item in the items array can be a string representing the document path, or another category object.

Step 3: Using the Sidebar

Once you’ve defined your sidebar structure, Docusaurus will automatically render it when you build and serve your site. Users will see a clear hierarchy in the sidebar, allowing them to expand and collapse categories for easier navigation.

Conclusion

By structuring your sidebars.js file appropriately, you can create a well-organized sidebar with nested categories in Docusaurus. This enhances the user experience by making it easier to find related documentation within a logical hierarchy.

====

To add an icon to a sidebar category in Docusaurus, you need to use the icon property within the category definition in your sidebars.js file. Here’s how you can do it step by step:

Step 1: Prepare Your Icon

  1. Choose an Icon: You can use an SVG or a PNG icon. Make sure it is stored in your project's static assets, typically under the static directory.

  2. Path to Icon: The path should be relative to the root of your Docusaurus project. For example, if your icon is stored as static/img/my-icon.svg, you would reference it as /img/my-icon.svg.

Step 2: Modify Your sidebars.js

Here’s an example of how to add an icon to a sidebar category:

module.exports = {
someSidebar: {
'Getting Started': [
{
type: 'category',
label: 'Installation',
icon: 'img/my-icon.svg', // Path to your icon
items: [
'installation/quickstart',
'installation/advanced',
],
},
{
type: 'category',
label: 'Usage',
icon: 'img/usage-icon.svg', // Another icon for a different category
items: [
'usage/basic-commands',
'usage/basic-tips',
],
},
],
},
};

Step 3: Explanation

  • icon Property: The icon property is added to the category object. Here you specify the path to the icon image you want to display alongside the category label.

  • Rendering: Docusaurus will render the icon next to the category label in the sidebar when you build and serve your site.

Step 4: Verify Your Icon

  1. Build and Serve: Run your Docusaurus site using the command:

    npm run start

    or

    yarn start
  2. Check the Sidebar: Navigate to your documentation site and check the sidebar to see if the icons appear next to the corresponding categories.

If your icon is in a subfolder within the static directory, you can still reference it in your sidebars.js file by providing the correct relative path. Here’s how to do it:

Step 1: Organize Your Icon

  1. Store Your Icon: Ensure your icon is placed in a subfolder within the static directory. For example:

    static/
    └── icons/
    └── my-icon.svg

Step 2: Reference the Icon in sidebars.js

When referencing the icon in your sidebars.js file, include the subfolder in the path. For example:

module.exports = {
someSidebar: {
'Getting Started': [
{
type: 'category',
label: 'Installation',
icon: 'img/icons/my-icon.svg', // Path to your icon in the subfolder
items: [
'installation/quickstart',
'installation/advanced',
],
},
{
type: 'category',
label: 'Usage',
icon: 'img/icons/usage-icon.svg', // Another icon in the same subfolder
items: [
'usage/basic-commands',
'usage/basic-tips',
],
},
],
},
};

Step 3: Explanation

  • Path Structure: When you define the icon property, the path should start with img/, followed by the subfolder name (icons/ in this case), and then the icon file name (my-icon.svg).

  • Rendering: Docusaurus will correctly resolve the path and render the icon next to the category label in the sidebar.

Step 4: Verify the Icon

  1. Build and Serve: After updating your sidebars.js, run your Docusaurus site:

    npm run start

    or

    yarn start
  2. Check the Sidebar: Go to your documentation site and confirm that the icons appear correctly next to the categories.

If your icons are in a different directory than the static directory, you won't be able to reference them directly in the same way as you would for files located in static. Docusaurus serves files in the static directory directly under the root URL, so icons must be accessible from there.

Options for Using Icons in a Different Directory

  1. Move Icons to the Static Directory:

    • The simplest solution is to move your icons into the static directory or a subfolder within it (e.g., static/icons/).

    • After moving, you can reference them as follows:

      icon: 'img/icons/my-icon.svg', // Example path after moving
  2. Use a URL:

    • If your icons are hosted externally (e.g., on a CDN or another server), you can use the full URL to reference them. For example:

      icon: 'https://example.com/path/to/my-icon.svg',
  3. Custom Webpack Configuration:

    • If you have a specific need to keep your icons in a different directory within your project, you could set up a custom Webpack configuration. However, this is more complex and generally not recommended unless necessary.
    • You would need to configure Docusaurus to include the directory in the build process, which could involve creating a custom plugin.

For ease of use and to avoid complications, the best practice is to store your icons in the static directory. Here's how you can do that:

  1. Organize Your Icons:

    • Place your icons in static/icons/.
  2. Reference Them in sidebars.js:

    module.exports = {
    someSidebar: {
    'Getting Started': [
    {
    type: 'category',
    label: 'Installation',
    icon: 'img/icons/my-icon.svg', // Path after moving to static
    items: [
    'installation/quickstart',
    'installation/advanced',
    ],
    },
    ],
    },
    };

Conclusion

To use icons effectively in Docusaurus, it’s best to keep them in the static directory. If you need to use icons from a different location, consider moving them to static or using a URL for external icons. This ensures that your icons are served correctly and simplifies your configuration.

Conclusion

By following this approach, you can easily use icons stored in subfolders within the static directory in your Docusaurus sidebar. Just ensure that the path you provide reflects the actual folder structure accurately.

By following these steps, you can easily add icons to your sidebar categories in Docusaurus, enhancing the visual appeal and usability of your documentation site. Make sure to use appropriate icons that represent the content of each category for better user experience.